/*-
 * Copyright (c) 2012-2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS: mtblas_1_nrm.c    Rev. 0.0.12    2013-10-17T17:11+08:00 $
 */

#include <stdlib.h>
#ifdef TRACE_INFO
#include <stdio.h>
#endif

#include <bscerr.h>

#include <exec_isa.h>
#include <mtblas.h>

int mtblas_nrm2f(size_t thrz, const size_t sz, const float *vx, const step_t incx, float *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    float *cbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (float*) calloc(thrz, sizeof(float));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, rb);
    ierr = operands_collector(thrz, sizeof(float), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_ff, sz, ra, rb, rc, NULL);

    *rbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(float);
    re.addr = rbuf;
    re.step = 0;

    execute((void*) op3_add_ff, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrtf(*rbuf);

    return ierr;
}


int mtblas_nrm2(size_t thrz, const size_t sz, const double *vx, const step_t incx, double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    double *cbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (double*) calloc(thrz, sizeof(double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, rb);
    ierr = operands_collector(thrz, sizeof(double), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_df, sz, ra, rb, rc, NULL);

    *rbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(double);
    re.addr = rbuf;
    re.step = 0;

    execute((void*) op3_add_df, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrt(*rbuf);

    return ierr;
}


int mtblas_nrm2l(size_t thrz, const size_t sz, const long double *vx, const step_t incx, long double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    long double *cbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (long double*) calloc(thrz, sizeof(long double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, rb);
    ierr = operands_collector(thrz, sizeof(long double), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_lf, sz, ra, rb, rc, NULL);

    *rbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(long double);
    re.addr = rbuf;
    re.step = 0;

    execute((void*) op3_add_lf, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrtl(*rbuf);

    return ierr;
}


int mtblas_cnrm2f(size_t thrz, const size_t sz, const float complex *vx, const step_t incx, float *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    float complex *cbuf;
    float complex tbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (float complex*) calloc(thrz, sizeof(float complex));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, rb);
    ierr = operands_collector(thrz, sizeof(float complex), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_conjfmac_fc, sz, ra, rb, rc, NULL);

    tbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(float complex);
    re.addr = &tbuf;
    re.step = 0;

    execute((void*) op3_add_fc, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrtf(crealf(tbuf));

    return ierr;
}


int mtblas_cnrm2(size_t thrz, const size_t sz, const double complex *vx, const step_t incx, double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    double complex *cbuf;
    double complex tbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (double complex*) calloc(thrz, sizeof(double complex));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, rb);
    ierr = operands_collector(thrz, sizeof(double complex), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_conjfmac_dc, sz, ra, rb, rc, NULL);

    tbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(double complex);
    re.addr = &tbuf;
    re.step = 0;

    execute((void*) op3_add_dc, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrt(creal(tbuf));

    return ierr;
}


int mtblas_cnrm2l(size_t thrz, const size_t sz, const long double complex *vx, const step_t incx, long double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    long double complex *cbuf;
    long double complex tbuf;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);
    cbuf = (long double complex*) calloc(thrz, sizeof(long double complex));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, rb);
    ierr = operands_collector(thrz, sizeof(long double complex), (void*) cbuf, rc);

    ierr = thread_execute(thrz, (void*) op3_conjfmac_lc, sz, ra, rb, rc, NULL);

    tbuf = 0;

    rd.addr = cbuf;
    rd.step = sizeof(long double complex);
    re.addr = &tbuf;
    re.step = 0;

    execute((void*) op3_add_lc, thrz, &re, &rd, &re, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    free(cbuf);

    *rbuf = sqrtl(creall(tbuf));

    return ierr;
}


int mtblas_asumf(size_t thrz, const size_t sz, const float *vx, const step_t incx, float *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    float *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (float*) calloc(thrz, sizeof(float));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, ra);
    ierr = operands_collector(thrz, sizeof(float), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_ff, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(float);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_ff, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_asum(size_t thrz, const size_t sz, const double *vx, const step_t incx, double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    double *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (double*) calloc(thrz, sizeof(double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, ra);
    ierr = operands_collector(thrz, sizeof(double), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_df, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(double);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_df, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_asuml(size_t thrz, const size_t sz, const long double *vx, const step_t incx, long double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    long double *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (long double*) calloc(thrz, sizeof(long double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, ra);
    ierr = operands_collector(thrz, sizeof(long double), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_lf, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(long double);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_lf, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_casumf(size_t thrz, const size_t sz, const float complex *vx, const step_t incx, float *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    float *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (float*) calloc(thrz, sizeof(float));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, ra);
    ierr = operands_collector(thrz, sizeof(float), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_fc, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(float);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_ff, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_casum(size_t thrz, const size_t sz, const double complex *vx, const step_t incx, double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    double *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (double*) calloc(thrz, sizeof(double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, ra);
    ierr = operands_collector(thrz, sizeof(double), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_dc, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(double);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_df, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_casuml(size_t thrz, const size_t sz, const long double complex *vx, const step_t incx, long double *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    long double *cbuf;
    operand_t rc;
    operand_t rd;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);
    cbuf = (long double*) calloc(thrz, sizeof(long double));

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, ra);
    ierr = operands_collector(thrz, sizeof(long double), (void*) cbuf, rb);

    ierr = thread_execute(thrz, (void*) op2_vabssum_lc, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = cbuf;
    rc.step = sizeof(long double);
    rd.addr = rbuf;
    rd.step = 0;

    execute((void*) op3_add_lf, thrz, &rc, &rd, &rd, NULL);

    ierr = operands_delete_2(ra, rb);

    free(cbuf);

    return ierr;
}


int mtblas_iamaxf(size_t thrz, const size_t sz, const float *vx, const step_t incx, sidx_t *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    operand_t rc;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, ra);
    ierr = operands_batch_set(thrz, (void*) vx, 0, rb);

    ierr = thread_execute(thrz, (void*) op2_vabsmax_ff, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = (void*) op2_vabsmax_ff;
    rc.step = 0;
    rd.addr = (void*) rb;
    rd.step = sizeof(operand_t);
    re.addr = (void*) rb;
    re.step = 0;

    execute((void*) op3_eoc2_op, thrz, &rc, &rd, &re, NULL);

    *rbuf = (sidx_t) (((float*) ((operand_t*) re.addr)->addr) - vx) / incx;

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_iamax(size_t thrz, const size_t sz, const double *vx, const step_t incx, sidx_t *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    operand_t rc;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, ra);
    ierr = operands_batch_set(thrz, (void*) vx, 0, rb);

    ierr = thread_execute(thrz, (void*) op2_vabsmax_df, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = (void*) op2_vabsmax_df;
    rc.step = 0;
    rd.addr = (void*) rb;
    rd.step = sizeof(operand_t);
    re.addr = (void*) rb;
    re.step = 0;

    execute((void*) op3_eoc2_op, thrz, &rc, &rd, &re, NULL);

    *rbuf = (sidx_t) (((double*) ((operand_t*) re.addr)->addr) - vx) / incx;

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_iamaxl(size_t thrz, const size_t sz, const long double *vx, const step_t incx, sidx_t *rbuf)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    operand_t rc;
    operand_t rd;
    operand_t re;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, ra);
    ierr = operands_batch_set(thrz, (void*) vx, 0, rb);

    ierr = thread_execute(thrz, (void*) op2_vabsmax_lf, sz, ra, rb, NULL, NULL);

    *rbuf = 0;

    rc.addr = (void*) op2_vabsmax_lf;
    rc.step = 0;
    rd.addr = (void*) rb;
    rd.step = sizeof(operand_t);
    re.addr = (void*) rb;
    re.step = 0;

    execute((void*) op3_eoc2_op, thrz, &rc, &rd, &re, NULL);

    *rbuf = (sidx_t) (((long double*) ((operand_t*) re.addr)->addr) - vx) / incx;

    ierr = operands_delete_2(ra, rb);

    return ierr;
}
